package com.leo.oplayer.view;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.leo.oplayer.R;
import com.leo.oplayer.adapter.WhitePersonAdapter;
import com.leo.oplayer.dao.ConfigCenter;
import com.leo.oplayer.dao.DBService;
import com.leo.oplayer.model.WhitePerson;
import com.leo.oplayer.util.Constancts;

public class ConfigView extends Activity{
	
	private DBService service;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.config_view);
		service = new DBService(this);
		
		String maxDay = ConfigCenter.getValue(this, Constancts.KEY_MAX_NOCONTACT_DAY, "7");
		String alarmHour = ConfigCenter.getValue(this, Constancts.KEY_ALARM_TIME_HOUR, "19");
		String alarmMinutes = ConfigCenter.getValue(this, Constancts.KEY_ALARM_TIME_MINUTES, "00");
		
		EditText v_maxDay = (EditText) findViewById(R.id.maxInterval);
		EditText v_hour = (EditText) findViewById(R.id.hours);
		EditText v_minutes = (EditText) findViewById(R.id.minutes);
		Button save = (Button) findViewById(R.id.saveConfig);
		
		ListView list = (ListView) findViewById(R.id.whiteList);
		List<WhitePerson> dataList = service.searchWhiteList(null);
		ArrayAdapter<WhitePerson> adapter = new WhitePersonAdapter(this,R.layout.white_person,dataList);    
		list.setAdapter(adapter);
		
		v_maxDay.setText(maxDay);
		v_hour.setText(alarmHour);
		v_minutes.setText(alarmMinutes);
		save.setOnClickListener(new OnClickListener(){
	    	public void onClick(View v){   
	    		
	    		EditText v_maxDay = (EditText) findViewById(R.id.maxInterval);
	    		EditText v_hour = (EditText) findViewById(R.id.hours);
	    		EditText v_minutes = (EditText) findViewById(R.id.minutes);
	    		
	    		String maxDay = v_maxDay.getText().toString();
	    		String alarmHour = v_hour.getText().toString();
	    		String alarmMinutes = v_minutes.getText().toString();
	    		String regex = "[0-9]*";
	    		String msg = "";
	    		
	    		
	    		if(maxDay==null || alarmHour == null || alarmMinutes == null){
	    			return;
	    		}
	    		if(!maxDay.matches(regex)){
	    			return;
	    		}else if(!alarmHour.matches(regex) || Integer.parseInt(alarmHour)>24){
	    			return;
	    		}else if(!alarmMinutes.matches(regex) || Integer.parseInt(alarmMinutes)>60){
	    			return;
	    		}
	    		
	    		//保存值   
	    		saveConfig(Constancts.KEY_MAX_NOCONTACT_DAY, maxDay);
	    		saveConfig(Constancts.KEY_ALARM_TIME_HOUR, alarmHour);
	    		saveConfig(Constancts.KEY_ALARM_TIME_MINUTES, alarmMinutes);
	    		
	    		msg = "save successful!";
				Toast.makeText(v.getContext(), msg, Toast.LENGTH_LONG).show();
	    		
	    	}
	    });
	}
	
	private void saveConfig(String key,String value){
		ConfigCenter.setValue(this, key, value);
	}

}
